package com.xiaren.ui;

import java.awt.Color;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.plaf.basic.BasicBorders;

import com.xiaren.common.IConstant;
import com.xiaren.common.ObjectConstant;

/**
 * @主页 http://www.leaf123.cn/
 * @邮箱 xia5523@qq.com
 */
public class MainJPanel extends JPanel implements ComResizedListener {

	public LeftJPanel leftPanel = new LeftJPanel(this);
	public CenterJPanel centerJPanel = new CenterJPanel();
	public JScrollPane jsp = new JScrollPane();
	public JToolBar toolBar = new JToolBar();
	public AbstractFrame mainFrame;

	public MainJPanel(AbstractFrame mainFrame) {
		this.mainFrame = mainFrame;
	}

	void initCommpent() {
		this.setLayout(null);
		toolBar.add(ObjectConstant.btnSave = new JButton("Save"));
		toolBar.add(ObjectConstant.btnSaveAll = new JButton("Save All"));
		toolBar.add(ObjectConstant.btnExit = new JButton("Exit"));
		toolBar.add(ObjectConstant.btnHelp = new JButton("Help"));
		toolBar.add(ObjectConstant.btnVisible = new JButton("Close"));
		toolBar.add(ObjectConstant.btnIcon = new JButton("Float Icon"));

		toolBar.setFloatable(false);
		toolBar.setBounds(0, 0, this.getWidth(), IConstant.TOOLBAR_HEIGHT);
		toolBar.setBorder(BasicBorders.getMenuBarBorder());
		leftPanel.setBounds(0, IConstant.TOOLBAR_HEIGHT,
				IConstant.LEFTPANEL_WIDTH, this.getHeight()
						- IConstant.TOOLBAR_HEIGHT);

		centerJPanel.setBounds(IConstant.LEFTPANEL_WIDTH,
				IConstant.TOOLBAR_HEIGHT, this.getWidth()
						- IConstant.LEFTPANEL_WIDTH, this.getHeight());

		this.add(leftPanel);
		this.add(toolBar);
		// this.setBackground(Color.PINK);
		this.add(centerJPanel);

	}

	public void init() {
		initCommpent();
		this.addComponentListener(new ComResizedListener());
		this.leftPanel.init();
		this.centerJPanel.init();
	}

	public void resized(ComponentEvent e) {
		// toolBar
		toolBar.setSize(e.getComponent().getWidth(), IConstant.TOOLBAR_HEIGHT);
		leftPanel.resized(e);
		centerJPanel.resized(e);
	}

	public CenterJPanel getCenterJPanel() {
		return centerJPanel;
	}

	class ComResizedListener implements ComponentListener {

		@Override
		public void componentResized(ComponentEvent e) {
			MainJPanel.this.resized(e);

		}

		@Override
		public void componentMoved(ComponentEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void componentShown(ComponentEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void componentHidden(ComponentEvent e) {
			// TODO Auto-generated method stub

		}

	}

	public AbstractFrame getMainFrame() {
		return mainFrame;
	}

}
